package com.rd.p2p.app.article;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.ArticleService;
import com.qdlc.p2p.biz.service.SiteService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.Site;
import com.qdlc.p2p.dal.model.ArticleModel;
import com.qdlc.p2p.dal.model.SiteModel;

public class AppArticleAction extends BaseAction<ArticleModel> implements ModelDriven<ArticleModel>{
	
	private SiteService siteService;
	
	private ArticleService articleService;
	
	private Map<String,Object> data;
	
	/**
	 * 文章列表 Ajax异步获取
	 * 
	 * @throws Exception
	 */
	@Action("/app/article/list")
	public void list() throws Exception {
		data = new HashMap<String, Object>();
		Site site = siteService.findByNid(model.getNid());
		SiteModel m = SiteModel.instance(site, model.getNid());
		m.validSiteForArticleList();
		model.setNid(site.getNid());
		model.setSize(site.getSize());
		PageDataList<ArticleModel> pageDataList = articleService.list(model);
		data.put("data", pageDataList);
		data.put("image_server_url",SystemConfigHelper.getValue(SystemConfigHelper.Nid.IMAGE_SERVER_URL));
		printWebJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 文章列表/详情页
	 * 
	 * @return
	 */
	@Action("/app/article/detail")
	public void detail() throws Exception {
		data = new HashMap<String,Object>();
		Site site = siteService.findByNid(model.getNid());
		List<Long> list = articleService.getIds(site);
		int index = list.indexOf(model.getId());
		if (index != 0 && index != -1) {
			model.setPrevId(list.get(index - 1));
		}
		if (index != list.size()-1 && index != -1) {
			model.setNextId(list.get(index + 1));
		}
		if (site == null) {
			data.put("data", site);
		}
		List<Site> parentList = new ArrayList<Site>();
		parentList = siteService.parentList(site, parentList);
		Map<String, Object> subListMap = siteService.subListMap(site, model);
		data.put("resCode", "1");
		data.put("data", site);
		data.put("parentList", parentList);
		data.put("subListMap", subListMap);
		printWebJson(getStringOfJpaObj(data));
	}
}
